using System;
using System.Data;
using System.Configuration;
using System.Web;

using NUnit.Framework;
using BTreeSharp;
using BTreeSharp.Surfaces;

namespace TheTest
{
#region dummy types
    class ComplexKey:IComparable
    {
        private string name;
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        private string surname;
        public string Surname
        {
            get { return surname; }
            set { surname = value; }
        }

        #region IComparable Members

        public int CompareTo(object obj)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        #endregion
    }
    class DummyType1
    {
        private int key;

        public int Key
        {
            get { return key; }
            set { key = value; }
        }
        private int v1;

        public int V1
        {
            get { return v1; }
            set { v1 = value; }
        }
        private long v2;

        public long V2
        {
            get { return v2; }
            set { v2 = value; }
        }
        private float v3;

        public float V3
        {
            get { return v3; }
            set { v3 = value; }
        }

        private double v4;

        public double V4
        {
            get { return v4; }
            set { v4 = value; }
        }

        private DateTime time;

        public DateTime V5
        {
            get { return time; }
            set { time = value; }
        }

    }
    class DummyType2
    {
        private ComplexKey key;

        public ComplexKey Key
        {
            get { return key; }
            set { key = value; }
        }
        private int v1;

        public int V1
        {
            get { return v1; }
            set { v1 = value; }
        }
        private long v2;

        public long V2
        {
            get { return v2; }
            set { v2 = value; }
        }
        private float v3;

        public float V3
        {
            get { return v3; }
            set { v3 = value; }
        }

        private double v4;

        public double V4
        {
            get { return v4; }
            set { v4 = value; }
        }

        private DateTime time;

        public DateTime V5
        {
            get { return time; }
            set { time = value; }
        }

    }
#endregion
    [TestFixture]
    public class BTree
    {
        [Test]
        public void InsertAndGet()
        {
            BTree<DummyType1,int> tree = BTree<DummyType1,int>.Create(new InMemorySurface(),8,1000,"Key");
            DummyType1 dt = new DummyType1();
            dt.Key = 1;
            dt.V1 = 1;
            dt.V2 = 2;
            dt.V3 = 3.5f;
            dt.V4 = 2.5;
            dt.V5 = DateTime.Now;
            tree.Insert(dt);
            DummyType1 res = tree.Get(dt.Key);
            Assert.AreEqual(dt.Key, res.Key);
            Assert.AreEqual(dt.V1, res.V1);
            Assert.AreEqual(dt.V2, res.V2);
            Assert.AreEqual(dt.V3, res.V3);
            Assert.AreEqual(dt.V4, res.V4);
            Assert.AreEqual(dt.V5, res.V5);
        }
        [Test]
        [ExpectedException(typeof(ObjectNotFoundException))]
        public void Delete()
        {
            BTree<DummyType1, int> tree = BTree<DummyType1, int>.Create(new InMemorySurface(), 8, 1000, "Key");
            DummyType1 dt = new DummyType1();
            dt.Key = 1;
            dt.V1 = 1;
            dt.V2 = 2;
            dt.V3 = 3.5f;
            dt.V4 = 2.5;
            dt.V5 = DateTime.Now;
            tree.Insert(dt);
            tree.Delete(dt.Key);
            Assert.IsNull(tree.Get(dt.Key,false));
            tree.Get(dt.Key);
        }
        [Test]
        [Ignore]
        public void InsertAndGetComplexKey()
        {
            BTree<DummyType2, ComplexKey> tree = BTree<DummyType2, ComplexKey>.Create(new InMemorySurface(), 8, 1000, "Key");
            DummyType2 dt = new DummyType2();
            dt.Key = new ComplexKey();
            dt.Key.Name = "name";
            dt.Key.Surname = "surname";
            dt.V1 = 1;
            dt.V2 = 2;
            dt.V3 = 3.5f;
            dt.V4 = 2.5;
            dt.V5 = DateTime.Now;
            tree.Insert(dt);
            DummyType2 res = tree.Get(dt.Key);
            Assert.AreEqual(dt.Key, res.Key);
            Assert.AreEqual(dt.V1, res.V1);
            Assert.AreEqual(dt.V2, res.V2);
            Assert.AreEqual(dt.V3, res.V3);
            Assert.AreEqual(dt.V4, res.V4);
            Assert.AreEqual(dt.V5, res.V5);
        }
        [Test]
        [ExpectedException(typeof(ObjectNotFoundException))]
        [Ignore]
        public void DeleteComplexKey()
        {
            BTree<DummyType2, ComplexKey> tree = BTree<DummyType2, ComplexKey>.Create(new InMemorySurface(), 8, 1000, "Key");
            DummyType2 dt = new DummyType2();
            dt.Key = new ComplexKey();
            dt.Key.Name = "name";
            dt.Key.Surname = "surname";
            dt.V1 = 1;
            dt.V2 = 2;
            dt.V3 = 3.5f;
            dt.V4 = 2.5;
            dt.V5 = DateTime.Now;
            tree.Insert(dt);
            tree.Delete(dt.Key);
            Assert.IsNull(tree.Get(dt.Key, false));
            tree.Get(dt.Key);
        }
    }
}
